
import vNode from './vNode'
import Watcher from './watcher'
export default class minVueTest {
    constructor({ template, data }) {
        //初始化获取app根节点
        this.rootEvent = document.getElementById("app")
        //初始化注册绑定数据
        this.data = this.newProxy(data);
        //生成定阅者
        this.watcher = new Watcher()
        //初始化生成Vnode根节点
        this.vNode = new vNode(template, this.watcher, this.data)
        //挂载节点
        this.rootEvent.appendChild(this.vNode.el.event.event)
    }
    newProxy(data = {}) {
        if (typeof data !== 'object' || data === null) {
            return data
        }
        let config = {
            get: (target, key, receiver) => {
                const ownKeys = Reflect.ownKeys(target)
                if (ownKeys.includes(key)) {
                    console.log(target, key, receiver)
                }
                const reflect = Reflect.get(target, key, receiver)
                this.watcher.addWatcher({ target: target, key: key })
                return this.newProxy(target[key])
            },
            set: (target, key, value, receiver) => {
                const reflect = Reflect.set(target, key, value, receiver)
                this.watcher.triggerWatcher(target, key, value,)
                return reflect
            },
            deleteProperty: (target, key) => {
                const reflect = Reflect.deleteProperty(target, key)
                console.log()
                return reflect
            },

        }
        let proxy = new Proxy(data, config);
        return proxy
    }
}